<template>
  <div class="box">
    <div class="main">
      <div class="tip">
        <img src="../../assets/tip.png"> 为保证您的帐号安全，更换手机号前需要进行安全验证
      </div>
      <template v-if="step===1">
        <div class="border">
          <label class="text">手机</label>
          <input type="text" v-model="stepOne.phone" placeholder="" readonly style="color:#ccc;">
        </div>
        <div class="border">
          <input type="text" v-model="stepOne.code" placeholder="请输入收到的短信验证码">
          <label class="send sending" v-if="stepOne.sending">{{stepOne.sending}}s</label>
          <label class="send" @click="sendCodeOne" v-else>发送验证码</label>
        </div>
        <div class="btn">
          <mt-button class="f14" type="primary" v-if="!stepOne.loading" @click="submitOne">
            <span>下一步</span>
          </mt-button>
          <mt-button class="f14" type="default" disabled v-else>
            <mt-spinner color="#333" :type="3"></mt-spinner>
          </mt-button>
        </div>
      </template>
      <template v-if="step===2">
        <div class="border">
          <label class="text">新手机号</label>
          <input type="text" v-model="stepTwo.phone" placeholder="">
        </div>
        <div class="border">
          <input type="text" v-model="stepTwo.code" placeholder="请输入收到的短信验证码">
          <label class="send sending" v-if="stepTwo.sending">{{stepTwo.sending}}s</label>
          <label class="send" @click="sendCodeTwo" v-else>发送验证码</label>
        </div>
        <div class="btn">
          <mt-button class="f14" type="primary" v-if="!stepTwo.loading" @click="submitTwo">
            <span>提交</span>
          </mt-button>
          <mt-button class="f14" type="default" disabled v-else>
            <mt-spinner color="#333" :type="3"></mt-spinner>
          </mt-button>
        </div>
      </template>
    </div>
  </div>
</template>
<script>
import { Toast } from 'mint-ui'
import * as api from '@/service/api'
export default {
  components: {
  },
  data () {
    const phone = this.$store.state.me.user.phone
    return {
      stepOne: {
        phone: phone,
        sending: 0,
        code: '',
        loading: false
      },
      stepTwo: {
        phone: '',
        sending: 0,
        code: '',
        loading: false
      },
      step: 1
    }
  },
  methods: {
    async sendCodeOne () {
      this.stepOne.sending = 60
      let totime = setInterval(() => {
        this.stepOne.sending--
        if (this.stepOne.sending < 1) {
          this.stepOne.sending = 0
          clearInterval(totime)
        }
      }, 1000)
      try {
        await api.redeemByPhone({
          phone: this.stepOne.phone
        })
      } catch (error) {
        console.log(error)
      }
    },
    async sendCodeTwo () {
      var status = true
      if (!/^\d{11}$/.test(this.stepTwo.phone)) {
        Toast('请输入正确的手机号码')
        status = false
      }
      if (!status) {
        return false
      }
      this.stepTwo.sending = 60
      let totime = setInterval(() => {
        this.stepTwo.sending--
        if (this.stepTwo.sending < 1) {
          this.stepTwo.sending = 0
          clearInterval(totime)
        }
      }, 1000)
      try {
        await api.getDynamicRegisterCode({
          phone: this.stepTwo.phone
        })
      } catch (error) {
        console.log(error)
      }
    },
    async submitOne () {
      var status = true
      if (!/^\d{6}$/.test(this.stepOne.code)) {
        Toast('短信验证码为6位数字')
        status = false
      }
      if (!status) {
        return false
      }
      this.stepOne.loading = true
      try {
        await api.verifyCode({
          type: 1,
          code: this.stepOne.code,
          phone: this.stepOne.phone
        })
        this.step = 2
      } catch (error) {
        console.log(error)
      }
      this.stepOne.loading = false
    },
    async submitTwo () {
      var status = true
      if (!/^\d{6}$/.test(this.stepTwo.code)) {
        Toast('短信验证码为6位数字')
        status = false
      } else if (!/^\d{11}$/.test(this.stepTwo.phone)) {
        Toast('请输入正确的手机号码')
        status = false
      }
      if (!status) {
        return false
      }
      this.stepTwo.loading = true
      try {
        await api.modifyUserPhone({
          userId: this.$store.state.me.user.userId,
          code: this.stepTwo.code,
          phone: this.stepTwo.phone
        })
        this.$store.dispatch('getUserInfo')
        Toast('手机号码修改成功')
        this.$router.push('/set/index')
      } catch (error) {
        console.log(error)
      }
      this.stepTwo.loading = false
    }
  }
}
</script>
<style lang="scss" scoped>
.box {
  padding-top: 5px;
  .main {
    background: #ffffff;
    padding: 0 30px;
  }
}
.tip {
  padding-top: 30px;
  font-size: 24px;
  color: #808080;
  display: flex;
  align-items: center;
  img {
    height: 36px;
    margin-right: 15px;
  }
}
.border {
  margin-top: 30px;
  border: 1px solid #e4e4e4;
  border-radius: 9px;
  padding: 20px;
  display: flex;
  align-items: center;
  .text {
    padding-right: 20px;
  }
  .send {
    padding-left: 20px;
    color: #186bd0;
    font-size: 28px;
    white-space: nowrap;
    border-left: 1px #e4e4e4 solid;
    text-align: center;
    &.sending {
      width: 6em;
      color: #ccc;
    }
  }
  input {
    flex: 1;
  }
}
.btn {
  padding: 30px 0;
  .mint-button {
    width: 100%;
    &.mint-button--primary {
      background: #f15d43;
      color: #fff;
    }
    display: flex;
    justify-content: center;
  }
}
</style>
